package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 湛江新闻网
 *
 */
public class YbxwwNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.ybxww.com/content/[0-9]+-[0-9]+/[0-9]+/[0-9]+.htm"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title =doc.select("td.bt2012").text();
		if (StringUtils.isBlank(title)) {
			title =doc.select("td.STYLE7").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("td.STYLE13").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("span.STYLE8").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("span.STYLE5").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("span.STYLE3").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("span.STYLE14").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("td.toutiao26").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("td.r_8 span").text();
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("strong.tit20").text();
		}
		date=DateUtils.matchDate(doc.select("td.hui12").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.STYLE5").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.STYLE2").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.STYLE3").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.STYLE6").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("span.STYLE8").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("span.STYLE13").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.STYLE10").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td[height=25]").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.hui12").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(doc.select("td.STYLE14").text());
		}
		content =HtmlCleaner.getContentHtml(url, doc.select("td#xhw"));
		if (StringUtils.isBlank(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("td.STYLE8"));
		}
		if (StringUtils.isBlank(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("td.txt14nr"));
		}
		if (StringUtils.isBlank(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("td.r_8"));
		}
		if (StringUtils.isBlank(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("td.neir"));
		}
		if (StringUtils.isBlank(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("td[align=left]"));
		}
		if (StringUtils.isBlank(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("td.STYLE12"));
		}
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	public static void main(String[] args) {
		YbxwwNewsAnalyse analyse=new YbxwwNewsAnalyse();
		String url="http://www.ybxww.com/content/2015-9/24/20159241535597070581.htm";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}

